package dao;

// Generated 2 juin 2012 17:41:31 by Hibernate Tools 3.4.0.CR1

import java.util.List;
import javax.naming.InitialContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.SessionFactory;

import dao.interfaces.IDaoBillet;

import beans.Aeroport;
import beans.Billet;
import static org.hibernate.criterion.Example.create;

/**
 * Home object for domain model class Billet.
 * @see beans.Billet
 * @author Hibernate Tools
 */
public class DaoBillet implements IDaoBillet {

	private static final Log log = LogFactory.getLog(DaoBillet.class);

	private final SessionFactory sessionFactory = getSessionFactory();

	protected SessionFactory getSessionFactory() {
		try {
			return HibernateUtil.getSessionFactory();
		} catch (Exception e) {
			log.error("Could not locate SessionFactory in JNDI", e);
			throw new IllegalStateException(
					"Could not locate SessionFactory in JNDI");
		}
	}

	/* (non-Javadoc)
	 * @see dao.IDaoBillet#persist(beans.Billet)
	 */
	@Override
	public void persist(Billet transientInstance) {
		log.debug("persisting Billet instance");
		try {
			sessionFactory.getCurrentSession().persist(transientInstance);
			log.debug("persist successful");
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			throw re;
		}
	}

	public void attachDirty(Billet instance) {
		log.debug("attaching dirty Billet instance");
		try {
			sessionFactory.getCurrentSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Billet instance) {
		log.debug("attaching clean Billet instance");
		try {
			sessionFactory.getCurrentSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see dao.IDaoBillet#delete(beans.Billet)
	 */
	@Override
	public void delete(Billet persistentInstance) {
		log.debug("deleting Billet instance");
		try {
			sessionFactory.getCurrentSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see dao.IDaoBillet#merge(beans.Billet)
	 */
	@Override
	public Billet merge(Billet detachedInstance) {
		log.debug("merging Billet instance");
		try {
			Billet result = (Billet) sessionFactory.getCurrentSession().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see dao.IDaoBillet#findById(int)
	 */
	@Override
	public Billet findById(int id) {
		log.debug("getting Billet instance with id: " + id);
		try {
			Billet instance = (Billet) sessionFactory.getCurrentSession().get(
					"beans.Billet", id);
			if (instance == null) {
				log.debug("get successful, no instance found");
			} else {
				log.debug("get successful, instance found");
			}
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<Billet> findByExample(Billet instance) {
		log.debug("finding Billet instance by example");
		try {
			List<Billet> results = (List<Billet>) sessionFactory
					.getCurrentSession().createCriteria("beans.Billet")
					.add(create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}
	
	public List<Billet> getAll(){
		Query q = HibernateUtil.getSessionFactory().getCurrentSession().createQuery("from Billet"); 
		List<Billet> liste = q.list();
		
		return liste;
	}
}
